package com.deyuanyun.pic.ctrl.validatefun;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.ctrl.annotation.DbTable;
import com.deyuanyun.pic.ctrl.domain.EqptCptestStation;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 测试桩注入
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/9/8）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class AutoPile implements ValidateInterface {
    @Override
    public <T, E, C> boolean apply(T t, E e, C c) throws NoSuchFieldException {
        Object obj= c;
        DbTable table=obj.getClass().getAnnotation(DbTable.class);
        Field f=(Field) e;
        String val= (String) t;
        if(null == val || "".equals(val))
        {
            return true;
        }


        String x=null;//勇于验证设备上的测试桩编号

        //如果有这个分隔，证明是有设备并且符号后面是测试桩的 前面是名称 后面是ID
        if(val.indexOf("_$_") > 0)
        {
            String vs[]=val.split("_\\$_",-1);
            val=vs[0];
            x=vs[1];
        }

        EQPTBaseService service= SpringAppliction.getBean(EQPTBaseService.class);
        EqptCptestStation device=service.queryObjectByCustomNumber(EqptCptestStation.class,val);
        try {
            if(x != null && device != null)
            {
                if(!device.getId().equals(x))
                {
                    BeanUtils.setProperty(c,f.getName(),val);
                    return false;
                }
            }



            try
            {
                if(device != null)
                {
                    BeanUtils.setProperty(c,f.getName(),device.getId());
                    BeanUtils.setProperty(c,"mileage",""+device.getMileage());
                }else{
                    BeanUtils.setProperty(c,f.getName(),val);
                    return false;
                }
            }catch (Exception e3)
            {
                e3.printStackTrace();
            }


            return true;
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        }

        return true;
    }

    @Override
    public String getName() {

        return null;
    }
}
